package org.elephas.webapp.frontend.component;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.validation.validator.EmailAddressValidator;
import org.apache.wicket.validation.validator.StringValidator;
import org.elephas.model.Blog;
import org.elephas.model.BlogConfiguration;
import org.elephas.model.User;
import org.elephas.service.Elephas;
import org.elephas.webapp.application.ElephasSession;
import org.elephas.webapp.frontend.component.common.form.ShinyForm;
import org.elephas.webapp.frontend.page.FrontPage;
import org.elephas.webapp.frontend.page.blog.BlogFrontPage;
import org.elephas.webapp.frontend.validator.UniqueBlogNameValidator;
import org.elephas.webapp.frontend.validator.UniqueEmailValidator;
import org.elephas.webapp.model.DetachableBlogModel;
import org.elephas.webapp.model.DetachableUserModel;

public class RegisterFormPanel extends Panel {
	private static final long serialVersionUID = 1L;

	@SpringBean
	private Elephas elephas;
	
	private boolean doCreateBlog = true;

	private String passwordCheck;

	public RegisterFormPanel(String id) {
		super(id);
		Form form = new ShinyForm("form");
		add(form);

		final WebMarkupContainer account = new WebMarkupContainer(
				"accountData", new CompoundPropertyModel(
						new DetachableUserModel()));
		form.add(account);
		account.add(new RequiredTextField("emailAddress").add(
				EmailAddressValidator.maximumLength(100)).add(
				new UniqueEmailValidator()));
		account.add(new RequiredTextField("firstName").add(StringValidator
				.maximumLength(30)));
		account.add(new RequiredTextField("lastName").add(StringValidator
				.maximumLength(30)));
		account.add(new PasswordTextField("password"));
		account.add(new PasswordTextField("passwordCheck", new PropertyModel(
				this, "passwordCheck")));

		final WebMarkupContainer blogData = new WebMarkupContainer("blogData",
				new CompoundPropertyModel(new DetachableBlogModel()));
		form.add(blogData);
		blogData.add(new CheckBox("createBlog", new PropertyModel(this,
				"doCreateBlog")));
		blogData.add(new RequiredTextField("name")
				.add(new UniqueBlogNameValidator()));
		blogData.add(new TextField("tagline"));
		blogData.add(new TextArea("description"));

		form.add(new Button("register") {
			private static final long serialVersionUID = 1L;

			@Override
			public void onSubmit() {
				User user = (User) account.getModelObject();
				elephas.saveUser(user);

				Blog blog = null;
				if (doCreateBlog) {
					blog = (Blog) blogData.getModelObject();
					blog.setConfiguration(new BlogConfiguration(blog));
					elephas.saveBlog(blog);
				}

				ElephasSession.get().signIn(user.getEmailAddress(),
						user.getPassword());

				if (doCreateBlog) {
					setResponsePage(BlogFrontPage.class, BlogFrontPage
							.constructPageParameters(blog));
				} else {
					setResponsePage(FrontPage.class);
				}
			}
		});
		form.add(new Button("cancel") {
			private static final long serialVersionUID = 1L;

			@Override
			public void onSubmit() {
				setResponsePage(FrontPage.class);
			}
		}.setDefaultFormProcessing(false));
	}
}
